from driver import GPIO
import yun_network
import yun_mqtt
import utime
import _thread

sta_led=GPIO()
sta_led.open("start_led")
net_led=GPIO()
net_led.open("net_led")


def sta_led_disp(id):
    while True:
        sta_led.write(1)
        utime.sleep_ms(200)
        sta_led.write(0)
        utime.sleep_ms(800)


    
def net_led_disp(id):
    while True:
        net_connect_flag=yun_network.getNetFlag()
        mqtt_connect_flag=yun_mqtt.get_mqtt_state()
        # 驻网失败/没有SIM卡/与基站脱离连接：300ms翻转一次电平
        if not net_connect_flag :
            net_led.write(1)
            utime.sleep_ms(300)
            net_led.write(0)
            utime.sleep_ms(300)  
    # 已驻网，未连云/与云脱离连接但还有网：1s翻转一次     
        elif net_connect_flag and not mqtt_connect_flag:
            net_led.write(1)
            utime.sleep_ms(1000)
            net_led.write(0)  
            utime.sleep_ms(1000) 
        # 连着云：常亮    
        elif net_connect_flag and mqtt_connect_flag:   
            net_led.write(1)
        # 勿删 
        utime.sleep_ms(10)    
  
       

def led_disp_thread():
    _thread.start_new_thread(sta_led_disp,(1,))
    _thread.start_new_thread(net_led_disp,(2,))
